package unreleased.geppetto.phraseHMM.phraseExtraction.phraseAccepter;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsCache;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.phraseAccepter.AbstractPhraseAccepter;

import java.io.IOException;



public class NoDanglingWords extends AbstractPhraseAccepter {

	AlignmentsSet _alignments;
	AlignmentsCache _cache;

	public NoDanglingWords(AlignmentsSet a) {
		_alignments = a;
		_cache = new AlignmentsCache(a);
	}

	// /**
	// * Does not accept null phrases
	// */
	// public boolean acceptNullPhrase(int sentenceNumber, byte sentenceSource,
	// int[] foreingSentence, int[] sourceSentence, int foreignStartPos, int
	// foreignEndPos, double score) {
	// return false;
	// }
/*
	public boolean acceptPhrase(int sentenceNumber, byte sentenceSource,
			int[] foreingSentence, int[] sourceSentence, int sourceStartPos,
			int sourceEndPos, int foreignStartPos, int foreignEndPos,
			double score) {
		if(score <= 0) return false;
		Alignment al = _alignments.get(sentenceNumber);
		for (int spos = sourceStartPos; spos <= sourceEndPos; spos++) {
			boolean hasSurePositions = false;
			for (int fpos = foreignStartPos; !hasSurePositions && fpos <= foreignEndPos; fpos++) {
				if (al.hasPosition(spos, fpos)) {
					hasSurePositions = true;
				}
			}
			if(!hasSurePositions){
				return false;
			}
		}
		
		for (int fpos = foreignStartPos;  fpos <= foreignEndPos; fpos++) {
			boolean hasSurePositions = false;
			for (int spos = sourceStartPos; !hasSurePositions && spos <= sourceEndPos; spos++) {
				if (al.hasPosition(spos, fpos)) {
					hasSurePositions = true;
				}
			}
			if(!hasSurePositions){
				return false;
			}
		}
		return true;
	}*/

	@Override
	public boolean acceptPhrase(ExtractedPhrasePair epp) {
		int sentenceNumber = epp.getSentenceNumber();
		int sourceEndPos = epp.getSourceEndPos();
		int sourceStartPos = epp.getSourceStartPos();
		int foreignEndPos = epp.getForeignEndPos();
		int foreignStartPos = epp.getForeignStartPos();
		Alignment al = _alignments.get(sentenceNumber);
		for (int spos = sourceStartPos; spos <= sourceEndPos; spos++) {
			boolean hasSurePositions = false;
			for (int fpos = foreignStartPos; !hasSurePositions && fpos <= foreignEndPos; fpos++) {
				if (al.hasPosition(spos, fpos)) {
					hasSurePositions = true;
				}
			}
			if(!hasSurePositions){
				return false;
			}
		}
		
		for (int fpos = foreignStartPos;  fpos <= foreignEndPos; fpos++) {
			boolean hasSurePositions = false;
			for (int spos = sourceStartPos; !hasSurePositions && spos <= sourceEndPos; spos++) {
				if (al.hasPosition(spos, fpos)) {
					hasSurePositions = true;
				}
			}
			if(!hasSurePositions){
				return false;
			}
		}
		return true;
		}

}
